package com.xtm.exercise.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 创建自定义的拦截器并实现HandlerInterceptor接口
 * 拦截器一

 过滤器和拦截器执行顺序： 先filter 后 interceptor
    过滤前-拦截前-action执行-拦截后-过滤后

 */
public class OneInterceptor implements HandlerInterceptor {
    private static final Logger LOGGER = LoggerFactory.getLogger(OneInterceptor.class.getName());

    /**
     * 预处理，controller方法执行前
     * return true 放行，执行下一个拦截器，如果没有，执行controller中的方法
     * return false不放行
     */
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
//        LOGGER.info("**********************interceptor拦截器开始--OneInterceptor**************************");
//
//        System.out.println("MyInterceptor1执行了...前1111....preHandle");
//        LOGGER.info("Intercepetor执行了，我是logger.info..........preHandle");
        String url =String.valueOf(request.getRequestURL()) ;
//        LOGGER.info("1、url=="+url);
        // 放开拦截
        return true;
    }

    /**
     * 后处理方法，controller方法执行后，success.jsp执行之前
     */
    @Override
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response,
                           Object handler, ModelAndView modelAndView) throws Exception {
//        System.out.println("MyInterceptor1执行了...后1111....postHandle");
//        LOGGER.info("1、postHandle");
    }

    /**
     * success.jsp页面执行后，该方法会执行
     */
    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response,
                                Object handler, Exception ex) throws Exception {
//        System.out.println("MyInterceptor1执行了...最后1111....afterCompletion");
//        LOGGER.info("1、afterCompletion");
    }
}
